package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.po.DistrictSimplePO;
import cn.tedu.tmall.front.mall.dao.cache.IDistrictCacheRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IReceiverAddressRepository;
import cn.tedu.tmall.front.mall.pojo.entity.ReceiverAddress;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IReceiverAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class ReceiverAddressService implements IReceiverAddressService {
    @Autowired
    private IReceiverAddressRepository receiverAddressRepository;
    @Autowired
    private IDistrictCacheRepository districtCacheRepository;

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, ReceiverAddressAddNewParam receiverAddressAddNewParam) {
        log.debug("开始处理添加收货地址的业务");
        // 省编码
        DistrictSimplePO province = districtCacheRepository.getByCode(receiverAddressAddNewParam.getProvinceCode());
        // 市编码
        DistrictSimplePO city = districtCacheRepository.getByCode(receiverAddressAddNewParam.getCityCode());
        if (province==null || city==null){
            String message="添加收货地址失败，选择的地区数据有误";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        ReceiverAddress receiverAddress=new ReceiverAddress();
        BeanUtils.copyProperties(receiverAddressAddNewParam,receiverAddress);
        receiverAddress.setUserId(currentPrincipal.getId());
        receiverAddress.setProvince(province.getName());
        receiverAddress.setCity(city.getName());
        // 获取区编码
        if (StringUtils.hasText(receiverAddressAddNewParam.getAreaCode())){
            DistrictSimplePO byCode = districtCacheRepository.getByCode(receiverAddressAddNewParam.getAreaCode());
            receiverAddress.setArea(byCode.getName());
        }

        // 如果新增的收货地址是“默认的” 则应该将原默认收货地址改为“非默认的
        Integer isDefault = receiverAddressAddNewParam.getIsDefault();
        if (isDefault.equals(DEFAULT_STATE_ON)){
            receiverAddressRepository.updateNotDefaultByUser(currentPrincipal.getId());
        }

        int insert = receiverAddressRepository.insert(receiverAddress);
        if (insert!=1){
            String message="添加收货地址失败,服务器繁忙";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public ReceiverAddressStandardVO getStandardById(CurrentPrincipal currentPrincipal, Long id) {
        ReceiverAddressStandardVO standard = receiverAddressRepository.getStandardById(id);
        // 判断当前id是否查询到数据
        if (standard==null){
            String message="查询收货地址失败,访问的数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        // 判断用户id是否与当事人id一样
        if (!standard.getUserId().equals(currentPrincipal.getId())){
            String message="查询收货地址失败,访问的数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return standard;
    }

    @Override
    public List<ReceiverAddressListItemVO> listByUser(CurrentPrincipal currentPrincipal) {
        return receiverAddressRepository.listByUser(currentPrincipal.getId());
    }
}
